package form;

import com.intellij.openapi.ui.Messages;
import org.apache.http.util.TextUtils;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Mvvm创建弹窗
 */
public class MvvmCreateDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK; // 确定
    private JButton buttonCancel; // 取消
    private JRadioButton activityRadioButton; // Activity
    private JRadioButton fragmentRadioButton; // Fragment
    private JRadioButton kotlinRadioButton; // Kotlin
    private JRadioButton javaRadioButton; // Java
    private JCheckBox isCreateModelCheckBox; // 是否创建Model
    private JTextField textName; // 类名


    public MvvmCreateDialog(Callback callback) {
        this.callback = callback;
        setTitle("Mvvm Create Helper");
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        // setSize(500, 300);
        setLocationRelativeTo(null);

        buttonOK.addActionListener(e -> onOK());

        buttonCancel.addActionListener(e -> onCancel());

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        // 默认选中创建Model
        isCreateModelCheckBox.setSelected(true);

        // 默认选中Activity
        activityRadioButton.setSelected(true);
        fragmentRadioButton.setSelected(false);
        activityRadioButton.addActionListener(e -> updateCreateType(activityRadioButton));
        fragmentRadioButton.addActionListener(e -> updateCreateType(fragmentRadioButton));

        // 默认选中Kotlin
        kotlinRadioButton.setSelected(true);
        javaRadioButton.setSelected(false);
        kotlinRadioButton.addActionListener(e -> updateCreateFileType(kotlinRadioButton));
        javaRadioButton.addActionListener(e -> updateCreateFileType(javaRadioButton));
    }

    /**
     * 修改创建文件类型Kotlin Or Java
     */
    private void updateCreateFileType(JRadioButton btn) {
        // 取消其他按钮选中
        kotlinRadioButton.setSelected(false);
        javaRadioButton.setSelected(false);
        // 选定当前点击按钮
        btn.setSelected(true);
    }

    /**
     * 修改选择创建类型
     *
     * @param btn 当前点击按钮
     */
    private void updateCreateType(JRadioButton btn) {
        // 取消其他按钮选中
        activityRadioButton.setSelected(false);
        fragmentRadioButton.setSelected(false);
        // 选定当前点击按钮
        btn.setSelected(true);
    }

    private void onOK() {
        String spaceHump = spaceHump(textName.getText());
        if (spaceHump.isEmpty()) {
            Messages.showInfoMessage("The input string cannot be empty", "Failure");
            return;
        }
        // Messages.showInfoMessage(spaceHump, "Create");
        if (callback != null) {
            CodeType codeType = CodeType.Activity;
            if (fragmentRadioButton.isSelected()) {
                codeType = CodeType.Fragment;
            }
            callback.onCreate(spaceHump, codeType, isCreateModelCheckBox.isSelected(), kotlinRadioButton.isSelected());
        }
        dispose();
    }

    /**
     * 空格转驼峰
     */
    private String spaceHump(String inputText) {
        // 去掉当前选中字符串的前后空格
        String trim = inputText.trim();
        // 判断去前后空格后字符串是否非空
        if (TextUtils.isEmpty(trim)) {
            return "";
        }
        if (trim.contains(" ")) {
            // 分割空格
            String[] split = trim.split(" ");
            trim = "";
            for (String s : split) {
                if (TextUtils.isEmpty(s)) {
                    continue;
                }
                trim += s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
            }
        }
        return trim.substring(0, 1).toUpperCase() + trim.substring(1);
    }

    private void onCancel() {
        dispose();
    }

    private Callback callback;

    /**
     * 回调函数
     */
    public interface Callback {
        /**
         * 确认回调
         *
         * @param className 类名
         * @param codeType  创建类型
         */
        void onCreate(String className, CodeType codeType, boolean isCreateModel, boolean isCreateKotlin);
    }

    public static void main(String[] args) {
        MvvmCreateDialog dialog = new MvvmCreateDialog((className, codeType, isCreateModel, isCreateKotlin) -> {

        });
        dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }
}